package com.studentsys.servlet;

import com.studentsys.bean.Student;
import com.studentsys.service.StudentService;
import com.studentsys.utils.FileUploadUtils;
import com.studentsys.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
@MultipartConfig
public class StudentServlet extends BaseServlet {
    private StudentService ss = new StudentService();

    //添加学生
    public void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //获取表单数据
        String sname = req.getParameter("sname");//姓名
        String age = req.getParameter("age");//年龄
        String sex = req.getParameter("sex");//性别
        String degree = req.getParameter("degree");//学历
        String email = req.getParameter("email");//邮箱
        String mark = req.getParameter("mark");//备注

        //获取图片文件
        Part part = req.getPart("pic");
        String pic = FileUploadUtils.uploadPic(part);

        //创建学生对象
        Student student = new Student(sname,Integer.parseInt(age),sex,pic,degree,email,mark);
        //调用StudentService中的添加方法
        int insert = ss.insert(student);
        //判断添加成功与否
        if (insert>0){
            resp.sendRedirect("student?method=findByPage");
        }else{
            resp.sendRedirect("addstudetn.jsp");
        }
    }
    //查询所有学生
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> list = ss.selectAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("studentlist.jsp").forward(req,resp);
    }
    //根据id查询学生
    public void selectOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int sid = Integer.parseInt(req.getParameter("sid"));
        Student student = ss.selectOne(sid);
        req.setAttribute("student",student);
        req.getRequestDispatcher("details.jsp").forward(req,resp);

    }
    //根据id修改学生
    public void updateShowStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        Student student = ss.selectOne(Integer.parseInt(sid));
        req.setAttribute("student",student);
        req.getRequestDispatcher("update.jsp").forward(req,resp);
    }
    //修改学生
    public void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取表单数据
        String sid = req.getParameter("sid");//学号
        String sname = req.getParameter("sname");//姓名
        String age = req.getParameter("age");//年龄
        String sex = req.getParameter("sex");//性别
        String pic = req.getParameter("oldpic");//图片
        String degree = req.getParameter("degree");//学历
        String email = req.getParameter("email");//邮箱
        String mark = req.getParameter("mark");//备注

        //更新头像
        Part part = req.getPart("pic");
        long size = part.getSize();
        if (size!=0){
            pic = FileUploadUtils.uploadPic(part);
        }

        Student stu = new Student(Integer.parseInt(sid),sname,Integer.parseInt(age),sex,pic,degree,email,mark);
        int i = ss.updateStudent(stu);
        if (i>0){
            resp.sendRedirect("student?method=findByPage");
        }
    }
    //根据id删除学生
    public void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        int i = ss.deleteStudent(Integer.parseInt(sid));
        if (i>0){
            resp.sendRedirect("student?method=findByPage");
        }
    }
    //检查邮箱唯一性
    public void checkEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取邮箱
        String email = req.getParameter("email");
        boolean flag = ss.checkEmail(email);
        //响应给客户端一个标识
        resp.getWriter().print(flag);
    }
    //检查姓名唯一性
    public void checkSname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sname = req.getParameter("sname");
        boolean flag = ss.checkSname(sname);
        //响应给客户端
        resp.getWriter().print(flag);
    }
    public void checkAge(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String age = req.getParameter("age");
        boolean flag = ss.checkAge(age);
        resp.getWriter().print(flag);

    }
    //分页查询
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询总记录数
        int totalCount = ss.getTotalCount();
        //获取当前页码
        String currentPage = req.getParameter("currentPage");
        //创建分页查询工具类对象
        PageUtils page = new PageUtils(totalCount,3,currentPage);
        //调用底层分页查询方法 list(3条数据)
        List<Student> list = ss.findByPage(page);
        //将list集合保存到page对象的list属性中
        page.setList(list);
        req.setAttribute("page",page);
        //转发到学生列表页面
        req.getRequestDispatcher("studentlist.jsp").forward(req,resp);
    }
}
